﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;

namespace ReveiwWord
{
    class HandleXML
    {
        private XmlDocument doc;

        public HandleXML()
        {
            if (File.Exists(@"Word.xml"))
            {
                doc = new XmlDocument();
                doc.Load(@"Word.xml");
            }
            else
            {
                doc = new XmlDocument();
                XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
                doc.AppendChild(dec);
               
                XmlElement root = doc.CreateElement("progress");
                doc.AppendChild(root);
                XmlElement element1 = doc.CreateElement("word");
                element1.SetAttribute("ID", "0");
                element1.SetAttribute("Data", DateTime.Now.ToString());
                element1.InnerText = "Comment";
                root.AppendChild(element1);

                doc.Save(@"Word.xml");
                Console.Write(doc.OuterXml);
            }
        }
        private void LoadXML()
        {       
           
        }
        private void ReadXML()
        {
            XmlNode xn = doc.DocumentElement.SelectSingleNode("word");       
//          Console.WriteLine(xn.Attributes["ID"].Value);           
        }
        public void InsertXML()
        {
        }
        public void DeleteXML()
        {
        }

        public string CurrentID()
        {
            string ID = doc.DocumentElement.SelectSingleNode("word").Attributes["ID"].Value;  
            return ID;
        }
        public string LastData()
        {
            string Data = doc.DocumentElement.SelectSingleNode("word").Attributes["Data"].Value;
            return Data;
        }
        public void WriteXML(string indexW, string data, string wordText)
        {
            XmlNode xn = doc.DocumentElement.SelectSingleNode("word");
            
                xn.Attributes["ID"].Value = indexW;
                xn.Attributes["Data"].Value = data;
                xn.InnerText = wordText;          
                doc.Save(@"Word.xml"); 
        }
    }
}
